Name Based Virtual Hosting
Name-based virtual hosting allows multiple websites (domains) to be hosted on a single IP address.
NGINX decides which website to serve based on the domain name requested by the client (the Host header in the HTTP request).
This is the most common hosting method today, especially when using shared servers or cloud VMs.
How Name-Based Virtual Hosting Works
Step-by-step request flow
- A user types a URL: http://example.com
- DNS resolves example.com → same server IP:
example.com → 203.0.113.10 - The browser sends an HTTP request including:
Host: example.com - NGINX:
- Reads the
Hostheader - Matches it against the
server_namedirective - Selects the correct
serverblock - Serves the corresponding website
- Reads the
Key Directives Used
| Directive | Purpose |
|---|---|
server {} | Defines a virtual host |
listen | IP and port to listen on |
server_name | Domain(s) this server block responds to |
root | Document root for the website |
index | Default index file |
Basic Example: Two Domains, One IP
Scenario
| Domain | Website |
|---|---|
example.com | Company website |
blog.example.com | Blog |
Both are hosted on one server IP.
http {
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name blog.example.com;
root /var/www/blog;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
}
-
listen 80;- Both sites listen on port 80
- Same IP, same port → name-based hosting
-
server_name example.com www.example.com;- Matches requests where:
Host: example.com
Host: www.example.com- You can list multiple domain aliases:
server_name blog.example.com; - Matches only the blog subdomain
-
root /var/www/example;- Files served from:
/var/www/example/index.html - Each server block has its own document root.
- Files served from:
-
Request Matching Behavior | Request | Served From | | ------------------------- | ------------------ | |
http://example.com|/var/www/example| |http://www.example.com|/var/www/example| |http://blog.example.com|/var/www/blog|
All requests go to the same IP, but different server blocks.
Default Server in Name-Based Hosting
If no server_name matches, NGINX serves the default server.
Default server example
server {
listen 80 default_server;
server_name _;
return 444;
}
- Prevents unknown domains from being served
- Improves security
- Common in production setups
Using Wildcards in server_name
server {
listen 80;
server_name *.example.com;
root /var/www/subdomains;
}
- Matches:
blog.example.com,shop.example.com - Does NOT match:
example.com
Using Regex in server_name
server {
listen 80;
server_name ~^www\d+\.example\.com$;
root /var/www/regex;
}
Matches: www1.example.com, www2.example.com
Name-Based Virtual Hosting with HTTPS (SNI)
Modern browsers support SNI (Server Name Indication), allowing name-based hosting with SSL.
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/example.crt;
ssl_certificate_key /etc/ssl/example.key;
}
Each domain can have its own certificate, even on the same IP.
When to Use Name-Based vs IP-Based Hosting
| Type | When to Use |
|---|---|
| Name-based | Most websites, shared servers |
| IP-based | Legacy SSL, strict isolation needs |